package seteam12;

import java.util.Random;

public class Generator implements IGenerator {

    @Override
    public void generateFill(SudokuBoard board, int dif) {
	int[][] matrix = board.getValues();
	Random rand = new Random();
	int pos, elements, no, xi, yi;
	
	int easy = 70;
	int medium = 40;
	int hard = 10;

	for (int i = 1; i <= 9; i++) {
	    no = rand.nextInt(80) + 1;
	    xi = no % 9;
	    yi = no / 9;
	    if (matrix[xi][yi] == 0)
		matrix[xi][yi] = i;
	    else
		i--;
	}
	board.setValues(matrix);
	board.setSolver(new BruteForceSolver());
	board.solve();

	
	elements = 81 - easy;
	
	
	//sets the number of empty fields for the different difficulty levels
	if(dif == 1){
		elements = 81 - easy;
	}
	
	if(dif == 2){
		elements = 81 - medium;
	}
	
	if(dif == 3){
		elements = 81 - hard;
	}
	//three different difficulty levels and random position (dancing links algorithm for the second requirement not required as yang li told us)
	matrix = board.getValues();
	for (int i = 0; i < elements; i++) {
	    pos = rand.nextInt(80) + 1;
	    xi = pos % 9;
	    yi = pos / 9;
	    if (matrix[xi][yi] != 0)
		matrix[xi][yi] = 0;
	    else
		i--;
	}
	board.setValues(matrix);
    }

}
